//
//  LoginView.m
//  OCProject
//
//  Created by 王明钦 on 2018/2/9.
//  Copyright © 2018年 oc. All rights reserved.
//

#import "LoginView.h"

@interface LoginView()

@property (strong, nonatomic) UITextField *userTF;
@property (strong, nonatomic) UITextField *pwdTF;

@end

@implementation LoginView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if(self){
        [self addUI];
    }
    return self;
    
}

-(void)addUI{
    
    UILabel *(^makeLab)()=^{
        UILabel *lab = [[UILabel alloc]init];
        lab.backgroundColor = SEPARATE_COLOR;
        return lab;
    };
    
    UIView *view = [[UIView alloc] init];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(30);
        make.right.equalTo(self.mas_right).offset(-30);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        
    }];
    
    UIImageView *logoIv = [[UIImageView alloc] init];
    logoIv.backgroundColor = NAV_BAR_COLOR;
    [self addSubview:logoIv];
    [logoIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(125, 125));
        make.top.equalTo(view.mas_top).offset(100);
        make.centerX.equalTo(self);
        
    }];
    
    UIImageView *icUser = [[UIImageView alloc] init];
    icUser.image = [UIImage imageNamed:@"denglu_yhm"];
    [self addSubview:icUser];
    [icUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(logoIv.mas_bottom).offset(58);
        make.left.equalTo(view.mas_left).offset(0);
        make.height.equalTo(@14);
        make.width.equalTo(@11);
        
    }];
    
    self.userTF = [[UITextField alloc] init];
    self.userTF.placeholder = @"用户名/手机号";
    self.userTF.font = FONT_28PX;
    [self addSubview:self.userTF];
    [self.userTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(logoIv.mas_bottom).offset(58);
        make.left.equalTo(icUser.mas_right).offset(15);
        make.right.equalTo(view.mas_right);
        
    }];
    
    UILabel *line1 = makeLab();
    [self addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userTF.mas_bottom).offset(9);
        make.left.equalTo(view);
        make.right.equalTo(view);
        make.height.equalTo(@1);
        
    }];
    
    UIImageView *icPwd = [[UIImageView alloc] init];
    icPwd.image = [UIImage imageNamed:@"denglu_mima"];
    [self addSubview:icPwd];
    [icPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1).offset(20);
        make.left.equalTo(view);
        make.height.equalTo(@14);
        make.width.equalTo(@11);
        
    }];
    
    self.pwdTF = [[UITextField alloc] init];
    self.pwdTF.font = FONT_28PX;
    self.pwdTF.placeholder = @"请输入密码";
    [self addSubview:self.pwdTF];
    [self.pwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1).offset(20);
        make.left.equalTo(icPwd.mas_right).offset(15);
        make.right.equalTo(view);
    }];
    
    UILabel *line2 = makeLab();
    [self addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pwdTF.mas_bottom).offset(8);
        make.left.equalTo(view);
        make.right.equalTo(view);
        make.height.equalTo(@1);
    }];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"登录" forState:UIControlStateNormal];
    [btn setTitleColor:WHITE_TITLE_COLOR forState:UIControlStateNormal];
    btn.titleLabel.font = FONT_36PX;
    btn.backgroundColor = NAV_BAR_COLOR;
    btn.layer.cornerRadius = BUTTON_CORNER_RADIUS;
    [btn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line2.mas_bottom).offset(30);
        make.left.equalTo(view);
        make.right.equalTo(view);
        make.height.offset(44);

    }];
    
    UIView *middle = [[UIView alloc] init];
    [self addSubview:middle];
    [middle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
    }];
    
    UILabel *forgetPwd = [[UILabel alloc] init];
    forgetPwd.text = @"忘记密码";
    forgetPwd.textColor = NAV_BAR_COLOR;
    forgetPwd.font = FONT_28PX;
    [self addSubview:forgetPwd];
    [forgetPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn.mas_bottom).offset(20);
        make.right.equalTo(middle).offset(-15);
        
    }];
    
    UILabel *registerLab = [[UILabel alloc] init];
    registerLab.text = @"立即注册";
    registerLab.textColor = NAV_BAR_COLOR;
    registerLab.font = FONT_28PX;
    [self addSubview:registerLab];
    
    NSLog(@"label frmae:%@", NSStringFromCGRect(registerLab.frame));
    
    [registerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn.mas_bottom).offset(20);
        make.left.equalTo(middle).offset(15);

    }];
    
//    UIView *LoginBgView = [[UIView alloc] init];
//    LoginBgView.backgroundColor = [UIColor grayColor];
//    LoginBgView.layer.cornerRadius = 2.0;
//    LoginBgView.layer.masksToBounds = YES;
//    [self addSubview:LoginBgView];
//    [LoginBgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(logoIv.mas_bottom).offset(63);
//        make.height.offset(88);
//
//    }];
    
}

-(void)loginAction{
    if (EMPTY(self.userTF.text)) {
        [SVProgressHUD showErrorWithStatus:@"请输入用户名"];
        return;
    }
    if (EMPTY(self.pwdTF.text)) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    if (self.btnCallBackHandle) {
        self.btnCallBackHandle();
    }
}

@end
